//
//  ZYModifySubAccountVC.m
//  YiLin
//
//  Created by Zyark on 2018/6/1.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYModifySubAccountVC.h"
#import "ZYViewUtils.h"
#import "ZYUser.h"
#import "ZYSubAccountViewModel.h"

@interface ZYModifySubAccountVC ()
@property(nonatomic,strong) UITextField *nameField;
@property(nonatomic,strong) UITextField *oldPasswordField;
@property(nonatomic,strong) UITextField *passwordField;
@property(nonatomic,strong) UITextField *rePasswordField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) ZYSubAccountViewModel *viewModel;
@end

@implementation ZYModifySubAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(_type == ZYModifySubAccountTypeEdit){
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_EDIT_SUBACCOUNT_TITLE];
    }else{
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_ADD_SUBACCOUNT_TITLE];
    }
}

#pragma mark - init
-(void)setup{
    [self setupTap];//点击关闭键盘
    [self setupView];
    [self bindView];
}

-(void)bindView{
    @weakify(self);
    //添加
    [self.viewModel.addCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        //添加成功
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ADD_SUB_ACCOUNT_SUCCESS]];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self.viewModel.addCommand.errors subscribeNext:^(id x) {
        @strongify(self);
        [ZYBasicHelper handleErrorMessaeWithError:x message:nil controller:self];
    }];
    //监听状态
    [[self.viewModel.addCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
    
    //修改
    [self.viewModel.modifyCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        //修改成功
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_MODIFY_SUB_ACCOUNT_SUCCESS]];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    //监听状态
    [[self.viewModel.modifyCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
    [self.viewModel.modifyCommand.errors subscribeNext:^(id x) {
        @strongify(self);
        [ZYBasicHelper handleErrorMessaeWithError:x message:nil controller:self];
    }];
}

-(void)setupView{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    CGFloat padding = 15;
    
    //用户名
    _nameField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_DATA_EDIT_USERNAME] placeholder:@""];
    [self.view addSubview:_nameField];
    _nameField.top = self.navigationView.bottom;
    _nameField.centerX = self.view.centerX;
    
    
    //原始密码
    _oldPasswordField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_OLD_PASSWORD_LABEL] placeholder:@""];
    [self.view addSubview:_oldPasswordField];
    _oldPasswordField.top = _nameField.bottom + padding;
    _oldPasswordField.centerX = self.view.centerX;
    _oldPasswordField.secureTextEntry = YES;
    
    //新密码
    _passwordField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_NEW_PASSWORD_LABEL] placeholder:@""];
    [self.view addSubview:_passwordField];
    _passwordField.top = _oldPasswordField.bottom + padding;
    _passwordField.centerX = self.view.centerX;
    _passwordField.secureTextEntry = YES;
    
    if(_type == ZYModifySubAccountTypeAdd){
        _oldPasswordField.hidden = YES;
        _passwordField.top = _nameField.bottom + padding;
    }else{
        _nameField.text = _account.username;
        _nameField.enabled = YES;
    }
    
    //确认密码
    _rePasswordField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_RE_PASSWORD_LABEL] placeholder:@""];
    [self.view addSubview:_rePasswordField];
    _rePasswordField.top = _passwordField.bottom + padding;
    _rePasswordField.centerX = self.view.centerX;
    _rePasswordField.secureTextEntry = YES;
    
    _confirmBtn = [ZYViewUtils setupConfirmBtn];
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:[language getStringByKey:LC_CONFIRM_BUTTON_TITLE] forState:UIControlStateNormal];
    _confirmBtn.centerX = self.view.centerX;
    _confirmBtn.top = _rePasswordField.bottom + 25;
    [_confirmBtn addTarget:self action:@selector(confirmModify:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -Event
-(void)confirmModify:(id)button{
    [self dismissKeyboard];
    if(_type == ZYModifySubAccountTypeAdd){
        ZYUser *user = [[ZYUser alloc] init];
        user.username = _nameField.text;
        user.password = _passwordField.text;
        user.rePassword = _rePasswordField.text;
        user.oldPassword = _oldPasswordField.text;
        [self.viewModel addSubAccount:user];
    }else{
        ZYUser *user = _account;
        user.username = _nameField.text;
        user.password = _passwordField.text;
        user.rePassword = _rePasswordField.text;
        user.oldPassword = _oldPasswordField.text;
        [self.viewModel modifySubAccount:user];
    }
}

#pragma mark -Getter
-(ZYSubAccountViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYSubAccountViewModel alloc] init];
    }
    return _viewModel;
}

@end
